$.dataTable = {
    index : 1,
    treeTemplate :
        '<ul id="treeSelect{id}" class="ztree"></ul>',


    /**
     * 初始化可移动树
     */
    initMovableTree : function(config) {


        // config.renameUrl = config.renameUrl || (config.urlPrefix + "/ajax/{id}/rename?newName={newName}");
        config.renameUrl = "ajax/{id}/rename?newName={newName}";
        // config.removeUrl = config.removeUrl || (config.urlPrefix + "/ajax/{id}/delete");
        config.removeUrl = "ajax/{id}/delete";
        // config.addUrl = config.addUrl || (config.urlPrefix + "/ajax/{id}/appendChild");
        config.addUrl =  "ajax/{id}/appendChild";
        // config.moveUrl = config.moveUrl || (config.urlPrefix + "/ajax/{sourceId}/{targetId}/{moveType}/move");
        config.moveUrl = "ajax/{sourceId}/{targetId}/{moveType}/move";
        config.asyncLoadAll = config.asyncLoadAll || false;
        config.loadUrl = config.loadUrl || (config.urlPrefix + "/ajax/load" +
                "?async=" + config.async +
                "&asyncLoadAll=" + config.asyncLoadAll +
                (config.excludeId ? "&excludeId=" + config.excludeId : "") +
                (config.onlyDisplayShow ? "&search.show_eq=true" : ""));

        if(config.editable != false) {
            config.editable = true;
        }

        if(!config.permission) {
            config.permission = {};
        }
        config.permission = $.extend({
            create: false,
            update: false,
            remove : false,
            move : false
        }, config.permission);


        var setting = {

            noSwitchIcon:true,
            async: {
                enable: config.async,
                url: config.loadUrl,
                autoParam:["id"],
                dataFilter: $.zTree.filter
            },
            view: {
                addHoverDom: config.permission.create ? addHoverDom : null,
                removeHoverDom: config.permission.create ? removeHoverDom : null,
                selectedMulti: false
            },
            edit: {
                enable: true,
                editNameSelectAll: true,
                showRemoveBtn : config.permission.remove ? function(treeId, treeNode) {return !treeNode.root;} : null,
                showRenameBtn: config.permission.update,
                removeTitle: "移除",
                renameTitle: "重命名",
                drag : {
                    isMove: config.permission.move,
                    isCopy : false,
                    prev: drop,
                    inner: drop,
                    next: drop
                }
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback:{
                beforeRemove: function(treeId, treeNode) { return confirm("确认删除吗？")},
                beforeRename : beforeRename,
                onRemove: onRemove,
                onRename: onRename,
                onDrop : onDrop
            }
        };

        if(!config.editable) {
            setting.edit = {};
            setting.view.addHoverDom = null;
            setting.view.removeHoverDom = null;
        }

        if(config,setting) {
            setting = $.extend(true, config.setting, setting);
            config.setting = setting;
        }

        function drop(treeId, nodes, targetNode) {
            if(!targetNode || !targetNode.getParentNode()) {
                return false;
            }
            for (var i = 0, l = nodes.length; i < l; i++) {
                if (nodes[i].root === true) {
                    return false;
                }
            }
            return true;
        }


        function addHoverDom(treeId, treeNode) {
            var sObj = $("#" + treeNode.tId + "_span");
            if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0) return;
            var addStr = "<span class='button add' id='addBtn_" + treeNode.id
                + "' title='添加子节点' onfocus='this.blur();'></span>";
            sObj.after(addStr);
            var btn = $("#addBtn_" + treeNode.id);
            if (btn)
                btn.bind("click", function (e) {
                    onAdd(e, treeId, treeNode);
                    return false;
                });
        }

        function removeHoverDom(treeId, treeNode) {
            $("#addBtn_" + treeNode.id).unbind().remove();
        }

        function beforeRename(treeId, treeNode, newName) {
            var oldName = treeNode.name;
            if (newName.length == 0) {
                $.app.alert({
                    message : "节点名称不能为空。"
                });
                return false;
            }
            if(!confirm("确认重命名吗？")) {
                var zTree = $.fn.zTree.getZTreeObj(treeId);
                zTree.cancelEditName(treeNode.name);
                return false;
            }
            return true;
        }
        /**
         * 重命名结束
         * @param e
         * @param treeId
         * @param treeNode
         */
        function onRename(e, treeId, treeNode) {
            var url = config.renameUrl.replace("{id}", treeNode.id).replace("{newName}",treeNode.name);
            $.app.waiting("操作中...", true);
            $.getJSON(url, function (data) {
                $.app.waitingOver();
            });
        }
        /**
         * 结束
         * @param e
         * @param treeId
         * @param treeNode
         */
        function onRemove(e, treeId, treeNode) {
            var url = config.removeUrl.replace("{id}", treeNode.id);
            $.app.waiting("操作中...", true);
            $.getJSON(url, function (data) {
                $.app.waitingOver();
            });
        }

        /**
         * 添加新节点
         * @param e
         * @param treeId
         * @param treeNode
         */
        function onAdd(e, treeId, treeNode) {
            var url = config.addUrl.replace("{id}", treeNode.id);
            $.app.waiting("操作中...", true);
            $.getJSON(url, function(newNode) {
                var node = { id:newNode.id, pId:newNode.pId, name:newNode.name, iconSkin:newNode.iconSkin, open: true,
                    click : newNode.click, root :newNode.root,isParent:newNode.isParent};
                var newNode = zTree.addNodes(treeNode, node)[0];
               zTree.selectNode(newNode);
                 $("#" + newNode.tId + "_a").click();
                $.app.waitingOver();
            });
        }



        var autocomplateEnable = config.autocomplete && config.autocomplete.enable;

        var id = this.index++;
        var treeStr = (autocomplateEnable ? this.autocompleteTemplate : '') + this.treeTemplate;
        var container = null;
        if(config.containerId) {
            container = $("#" + config.containerId);
        } else {
            container = $("body");
        }
        container.append(treeStr.replace(/{id}/g, id));
        var treeSelect = "treeSelect" + id;
        var zTree = $.fn.zTree.init($("#" + treeSelect), setting, config.zNodes);



        function getQueryCondition(data) {
            var param = {};
            //组装排序参数
            if (data.order && data.order.length && data.order[0]) {
                switch (data.order[0].column) {
                    case 1:
                        param.orderColumn = "name";
                        break;
                    case 2:
                        param.orderColumn = "position";
                        break;
                    case 3:
                        param.orderColumn = "status";
                        break;
                    case 4:
                        param.orderColumn = "start_date";
                        break;
                    default:
                        param.orderColumn = "name";
                        break;
                }
                param.orderDir = data.order[0].dir;
            }
            //组装查询参数
            if ($("#salePrice").val())
                param.salePrice = $("#salePrice").val();
            if ($("#areaSize").val())
                param.areaSize = $("#areaSize").val();
            if ($("#searchCommunity").val())
                param.community = $("#searchCommunity").val();
            if ($("#area_id").val())
                param.area_id = $("#area_id").val();

            //组装分页参数
            param.pageOffset = data.start;
            param.pageSize = data.length;
            return param;
        }



        var table = $('#houseTable').DataTable({
            language: {
                url: '../Chinese.json'
            },
            //responsive: true,
            colReorder: true,
            searching: false,
            ordering: false,
            "processing": true,
            "serverSide": true,
            "paging": true,
            "aLengthMenu": [[20, 50, -1], [20, 50, "显示所有"]], //这个是允许用户自定义每页数量的时候,下拉菜单的选项
            "bPaginate": true,      //显示分页器
            "bLengthChange": false, // 用户不可改变每页显示数量
            "iDisplayLength": 20,   //一页显示条数
            "ajax": {
                data: function (data) {
                    return getQueryCondition(data)
                },
                url: postUrl,
                type: "GET",
                dataSrc: function (data) {
                    data.recordsTotal = data.totalCount;
                    data.recordsFiltered = data.totalCount;
                    return data.data;
                }
            },
            "columns": [
                {"data": "createdate",
                    "render" : function(data,type, row, meta) {
                        return new Date(parseInt(data)).format("yyyy-MM-dd hh:mm");
                    }
                },
                {"data": "mobile"},
                {"data": "community"},
                {"data": "areasize"},
                {"data": "saleprice"},
                {"data": "ban"},
                {"data": "roomnumber"},
                {"data": null, className: "td-operation  text-center", orderable: false, width: "15%", defaultContent: ""}
            ],
            "createdRow": function (row, data, index) {
                //不使用【render】，改用jquery文档操作呈现单元格  data.id
                var $btnEdit = $('<button type="button" class="btn btn-small btn-primary btn-edit" onclick="queryData(' + data.id + ')">查看数据</button>');
                $('td', row).eq(7).append($btnEdit);
            }
        });
        return table;
    }

}